﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHModel.Entities;

namespace NHBLL
{
    public static class TeacherInfoService
    {
        public static IList<TeacherInfo> GetAll()
        {
            ISession session = NHelper.GetCurrentSession();
            IQuery iq = session.CreateQuery("from TeacherInfo");
            return iq.List<TeacherInfo>();
        }


        public static TeacherInfo GetModelByUserNameAndPass(string UserName, string PassWord)
        {
            ISession session = NHelper.GetCurrentSession();
            ICriteria crit = session.CreateCriteria(typeof(TeacherInfo));
            crit.Add(NHibernate.Criterion.Expression.Eq("UserName", UserName));
            crit.Add(NHibernate.Criterion.Expression.Eq("PassWord", PassWord));
            TeacherInfo item = (TeacherInfo)crit.UniqueResult();
            return item;         
        }

        public static TeacherInfo GetModelByID(int id)
        {
            ISession session = NHelper.GetCurrentSession();
            ICriteria crit = session.CreateCriteria(typeof(TeacherInfo));
            crit.Add(NHibernate.Criterion.Expression.Eq("id", id));
            TeacherInfo item = (TeacherInfo)crit.UniqueResult();
            return item;
        }

        public static bool Add(TeacherInfo u)
        {
            ISession session = NHelper.GetCurrentSession();
            ITransaction it = session.BeginTransaction();
            bool flag = true;
            try
            {
                session.Save(u);
                it.Commit();
            }
            catch (Exception ex)
            {
                flag = false;
                it.Rollback();
                throw (ex);
            }
            return flag;
        }

        public static bool Update(object obj)
        {
            bool flag = true;
            ISession session = NHelper.GetCurrentSession();

            ITransaction it = session.BeginTransaction();
            try
            {
                session.Update(obj);
                it.Commit();
            }
            catch (Exception)
            {
                flag = false;
                it.Rollback();
                throw;
            }
            session.Flush();
            return flag;
        }
        
        
    }
}
